package cn.wl.crm.web.controller;

import cn.wl.AjaxResult;
import cn.wl.PageList;
import cn.wl.crm.domain.Parts;
import cn.wl.crm.domain.PayType;
import cn.wl.crm.query.PartsQuery;
import cn.wl.crm.service.IPartsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/parts")
@Api(tags = "用户接口类")
@CrossOrigin
public class PartsController {

    @Autowired
    private IPartsService partsService;

    @RequestMapping(value = "/findAll",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    public List<Parts> findAll(){
        return partsService.queryAll();

    }
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    public PageList queryPage(@RequestBody PartsQuery partsQuery){
        PageList pageList = partsService.queryPage(partsQuery);
        System.out.println(pageList);
        return pageList;
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询用户",notes = "getOne查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
    })
    @ResponseBody
    public Parts getOne(@PathVariable Long id){
        return partsService.queryOne(id);
    }

    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult save(@RequestBody Parts parts){
        try {
            parts.setCreateTime(new Date());
            partsService.save(parts);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败！！！");
        }
        return new AjaxResult();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody Parts parts){
        try {
            parts.setCreateTime(new Date());
            partsService.update(parts);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败！！！");
        }
        return new AjaxResult();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult update(@PathVariable Long id){
        try {
            partsService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败！！！");
        }
        return new AjaxResult();
    }
}
